package com.study.modules.designPatterns.No06ObserverMode;

import java.util.ArrayList;
import java.util.List;

public class AbstractSubject implements Subject {

    private List<Observer> observers;

    public AbstractSubject() {
        this.observers = new ArrayList<>();
    }

    /**
     * 添加观察者
     */
    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    /**
     * 移除观察者
     */
    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    /**
     * 通知观察者
     */
    @Override
    public void notify1(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}
